import os
import re
import psutil
import subprocess
import time
weixin_name='Weixin.exe'
mutex_name='XWeChat_App_Instance_Identity_Mutex_Name'
 
def get_pid(name):
    """
    获取所有的微信进程
    """
    process_list = psutil.pids()
    pids = []
    for pid in process_list:
        try:
            if psutil.Process(pid).name() == name:
                pids.append(pid)
        except:
            continue
    return pids
 
 
def more_open():
    pids = get_pid(weixin_name)
    for pid in pids:
        #遍历所有微信的pid 把 Mutex都干掉
        cmd = f"handle -a -u -p {pid}"
        p = subprocess.Popen(cmd,
                     shell=True,
                     stdout=subprocess.PIPE
                     )
        if p.returncode == False:
            return
        # 输出stdout
        result = p.communicate()[0].decode("utf-8")
        search_result = ""
        for i in result.split("\n"):
            if i.strip():
                if i.strip().endswith(mutex_name):
                    search_result += i
        if not search_result:
            continue
        re_result = re.findall('([0-9A-Z]+): Mutant', search_result, re.S)
        #上面这个循环是匹配 Mutex的handle
        if re_result:
            for _id in re_result:
                subprocess.Popen(f'handle -p {pid} -c {_id} -y',shell=True).wait()

more_open()

 